package main

import (
	_ "github.com/Go-SQL-Driver/MySQL"
	"github.com/astaxie/beego/orm"
	"time"
	"fmt"
)

//与数据库学生表映射的结构体
type studentinfo struct {
	Id          int    `pk:"auto"`
	Stuname     string `orm:"size(20)"`
	Stuidentify string `orm:"size(30)"`
	Stubirth    time.Time
	Stuclass    string `orm:"size(30)"`
	Stumajor    string `orm:"size(30)"`
}

//数据库连对象需要的信息
var (
	dbuser string = "root"
	dbpwd  string = "root"
	dbname string = "gobeedb"
)

//初始化orm
func init() {
	conn := dbuser + ":" + dbpwd + "@(localhost:3306)/" + dbname + "?charset=utf8"//组合成连接串
	orm.RegisterModel(new(studentinfo))//注册表studentinfo 如果没有会自动创建
	orm.RegisterDriver("mysql", orm.DRMySQL) //注册mysql驱动
	orm.RegisterDataBase("default", "mysql", conn) //设置conn中的数据库为默认使用数据库
	orm.RunSyncdb("default", false, false)//后一个使用true会带上很多打印信息，数据库操作和建表操作的；第二个为true代表强制创建表
}


func main() {

	//origSql()
	ormBee();

}

func ormBee()  {

	orm.Debug = true
	dbObj := orm.NewOrm()
	var stuPtr *studentinfo = new(studentinfo)
	stuPtr.Stuname = "xiaom"
	stuPtr.Stubirth = time.Now()
	stuPtr.Stuclass = "一年级1班"
	stuPtr.Stuidentify = "1234"
	stuPtr.Stumajor = "计算机"

	tm := time.Now()
	var studentus = []studentinfo{
		{Stuname: "xd", Stuidentify: "1235", Stubirth: tm, Stuclass: "一年级2班", Stumajor: "数据库"},
		{Stuname: "xx", Stuidentify: "1236", Stubirth: tm, Stuclass: "一年级3班", Stumajor: "网络"},
		{Stuname: "xn", Stuidentify: "1237", Stubirth: tm, Stuclass: "一年级4班", Stumajor: "C语言"},
		{Stuname: "xb", Stuidentify: "1238", Stubirth: tm, Stuclass: "一年级5班", Stumajor: "JAVA"},
		{Stuname: "xq", Stuidentify: "1239", Stubirth: tm, Stuclass: "一年级6班", Stumajor: "C++"},
	}

	var err error
	_, err = dbObj.Insert(stuPtr)//单条记录插入
	if err != nil {
		fmt.Printf("插入学生:%s信息出错。\n", stuPtr.Stuname)
	} else {
		fmt.Printf("插入学生:%s信息成功。\n", stuPtr.Stuname)
	}

	var num int64
	num, err = dbObj.InsertMulti(2, studentus)//多条记录插入
	if err != nil {
		fmt.Printf("插入%d个学生信息错误,%d个学会信息成功。\n", 5-num, num)
	} else {
		fmt.Printf("成功插入%d学生信息。\n", num)
	}

	studentR := new(studentinfo)//记录读取，需要指定主键
	studentR.Id = 6
	err = dbObj.Read(studentR)
	if err != nil {
		fmt.Printf("读取ID:%d的学生信息失败", studentR.Id)
	} else {
		fmt.Printf("ID:%d的学生个人信息为：\n",studentR.Id)
		fmt.Println(studentR)
	}

	studentU := new(studentinfo)
	studentU.Id = 5
	studentU.Stumajor = "管理科学与工程"
	_, err = dbObj.Update(studentU, "Stumajor", "Stuclass")//记录更新
	if err != nil {
		fmt.Printf("更新ID:%d的学生信息失败。", studentU.Id)
	} else {
		fmt.Printf("更新ID:%d的学生信息成功。", studentU.Id)
	}



}

func origSql()  {


	orm.Debug = true
	dbObj := orm.NewOrm()
	sql := fmt.Sprintf("insert into studentinfo(Id,Stuname, Stuidentify, Stubirth, Stuclass, Stumajor)"+
		" values(1, 'rjx2','xxx319928xxx','%s','信管1班','信息管理与信息系统')", "1992-01-01 11:11:11")
	fmt.Println(sql)
	_, err := dbObj.Raw(sql).Exec()
	if err != nil {
		fmt.Printf("插入数据至：t_studentInfo出错\n--%v", err)
	}

	sql = fmt.Sprintf("insert into studentinfo(Id, Stuname, Stuidentify, Stubirth, Stuclass, Stumajor)"+
		" values(2, 'qcy','xxx319918xxx','%s','XXX','YYYYYY')", "1992-01-01 11:11:11")
	_, err = dbObj.Raw(sql).Exec()
	if err != nil {
		fmt.Println("插入数据至：t_studentInfo出错")
	}
	//更新数据
	sql = "update studentinfo set Stuname='qcym' where Id= 2"
	_, err = dbObj.Raw(sql).Exec()
	if err != nil {
		fmt.Println("更新t_studentInfo表出错")
	}
	//通过事务方式来进行数据插入
	err = dbObj.Begin()
	sql = fmt.Sprintf("insert into studentinfo(Id, Stuname, Stuidentify, Stubirth, Stuclass, Stumajor)"+
		" values(3, 'loe','xxx319918xxx','%s','zzzz','TTTT')", "1992-01-01 11:11:11")
	_, err = dbObj.Raw(sql).Exec()
	if err != nil {
		dbObj.Rollback()
		fmt.Println("插入t_studentInfo表出错,事务回滚")
	} else {
		dbObj.Commit()
		fmt.Println("插入t_studenInfo表成功,事务提交")
	}
	//查询数据库
	students := make([]studentinfo, 20)
	//var students []studentinfo
	sql = "select Id,Stuname,Stuidentify,Stubirth,Stuclass,Stumajor from studentinfo"
	fmt.Println(sql)
	num, er := dbObj.Raw(sql).QueryRows(&students)
	if er != nil {
		fmt.Println("查询学生信息出错")
	} else {
		fmt.Printf("从t_studenInfo表中共查询到记录:%d条\n", num)
		for index, _ := range students {
			fmt.Printf("第%d个学生个人信息：", index+1)
			fmt.Printf("姓名：%s,身份证号:%s，出生日期:%s,班级:%s,专业:%s", students[index].Stuname, students[index].Stuidentify, students[index].Stubirth, students[index].Stuclass, students[index].Stumajor)
		}
	}


}